const debounce = {
  inserted: function (el, binding) {
    let debounceTime = binding.value; // 节流时间
    if (!debounceTime) {
      // 用户若不设置防抖时间，则默认1s
      debounceTime = 1000;
    }
    let cbFun;
    el.addEventListener(
      'click',
      (event) => {
        if (!cbFun) {
          // 第一次执行
          cbFun = setTimeout(() => {
            cbFun = null;
          }, debounceTime);
        } else {
          // 阻止事件冒泡并且阻止该元素上同事件类型的监听器被触发
          event && event.stopImmediatePropagation();
        }
      },
      true
    );
  },
};

export default debounce;
